-------------------------------------------------------------------------------
--  The Legend of Zelda : Oracle of Secrets
--  Copyright (C) 2010 Pixelua
--
--  This program is free software: you can redistribute it and/or modify
--  it under the terms of the GNU General Public License as published by
--  the Free Software Foundation, either version 3 of the License, or
--  (at your option) any later version.
--
--  This program is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--  GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License
--  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------
NPC = {}

NPC.add = function(name, x, y, text, dir, map)
	NPC[name] = {
		x = x,
		y = y,
		text = text,
		spr = Sprite.new("img/Characters/"..name..".png", 16, 16, VRAM),
		dir = dir,
		map = map
	}
	
	NPC[name].spr:addAnimation({0, 4}, 200) -- Bas
	NPC[name].spr:addAnimation({1, 5}, 200) -- Droite
	NPC[name].spr:addAnimation({2, 6}, 200) -- Gauche
	NPC[name].spr:addAnimation({3, 7}, 200) -- Haut
end

NPC.blit = function(name)
	NPC[name].spr:playAnimation(SCREEN_DOWN, NPC[name].x, NPC[name].y, NPC[name].dir)
end

NPC.blitAll = function()
	for k, v in pairs(NPC) do
		if k ~= "add" and k ~= "blit" and k ~= "blitAll" then
			if NPC[k].map == actual_map_tab.coord then
				NPC.blit(k)
								
				if link.coordx == NPC[k].x-16 and link.coordy < NPC[k].y+16 and link.coordy+16 > NPC[k].y then
					link.blocked.right = true
					if Keys.held.Right and Keys.released.Down and passable(math.ceil((link.coordx + 1)/16), math.ceil((link.coordy)/16)) and link.coordy - NPC[k].y >= 5 then
						link.coordy = link.coordy + 1 
					elseif Keys.held.Right and Keys.released.Up and passable(math.ceil((link.coordx + 1)/16), math.floor((link.coordy-1)/16)) and NPC[k].y - link.coordy >= 5 then
						link.coordy = link.coordy - 1 
					end
				else
					link.blocked.right = false
				end
				
				if link.coordx == NPC[k].x+16 and link.coordy < NPC[k].y+16 and link.coordy+16 > NPC[k].y then
					link.blocked.left = true
					if Keys.held.Left and Keys.released.Down and passable(math.floor((link.coordx + 1)/16), math.ceil((link.coordy)/16)) and link.coordy - NPC[k].y >= 5 then
						link.coordy = link.coordy + 1 
					elseif Keys.held.Left and Keys.released.Up and passable(math.floor((link.coordx + 1)/16), math.floor((link.coordy-1)/16)) and NPC[k].y - link.coordy >= 5 then
						link.coordy = link.coordy - 1 
					end
				else
					link.blocked.left = false
				end
				
				if link.coordy == NPC[k].y+12 and link.coordx+16 > NPC[k].x and link.coordx < NPC[k].x+16 then
					link.blocked.up = true
					if Keys.held.Up and Keys.released.Left and passable(math.floor((link.coordx)/16),math.floor((link.coordy-1)/16)) and NPC[k].x - link.coordx >= 5 then
						link.coordx = link.coordx - 1 
					elseif Keys.held.Up and Keys.released.Right and passable(math.ceil((link.coordx + 1)/16),math.floor((link.coordy - 1)/16)) and link.coordx - NPC[k].x >= 5 then
						link.coordx = link.coordx + 1 
					end
				else
					link.blocked.up = false
				end
				
				if link.coordy+16 == NPC[k].y and link.coordx+16 > NPC[k].x and link.coordx < NPC[k].x+16 then
					link.blocked.down = true
					if Keys.held.Down and Keys.released.Left and passable(math.floor((link.coordx)/16),math.ceil((link.coordy + 1)/16)) and NPC[k].x - link.coordx >= 5 then
						link.coordx = link.coordx - 1 
					elseif Keys.held.Down and Keys.released.Right and passable(math.ceil((link.coordx + 1)/16),math.ceil((link.coordy + 1)/16)) and link.coordx - NPC[k].x >= 5 then
						link.coordx = link.coordx + 1 
					end
				else
					link.blocked.down = false
				end
				
				if link.blocked.up and NPC[k].dir == 1 then
					if Keys.newPress.A and inBoxText_Timer:time() == 0 then 
						ZE.printText(NPC[k].text, 1)
					end
				end
			end
		end
	end
end
